package com.drinks.yiniang.ui.widget.textview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.drinks.yiniang.R;
import com.hansen.library.utils.ScreenSizeUtils;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 绘制加减号view
 * Created by han on 2019/2/26 0026.
 */

public class SymbolsTextView extends View {

    public static final int TYPE_PLUS = 0x01;
    public static final int TYPE_MINUS = 0x02;

    private Context mContext;

    private int mTypes;
    private int mPaintWidth;
    private int mPadding;
    private int mPaintColor;

    private int mVStartX;
    private int mVStartY;
    private int mVEndX;
    private int mVEndY;

    private int mHStartX;
    private int mHStartY;
    private int mHEndX;
    private int mHEndY;
    private int dp20;

    private Paint mPaint;

    public SymbolsTextView(Context context) {
        this(context, null);
    }

    public SymbolsTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SymbolsTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mContext = context;

        dp20 = ScreenSizeUtils.dp2px(mContext, 20);//默认值

        init(attrs);
        initData();
    }

    private void init(AttributeSet attrs) {
        mPaintWidth = 2;
        mPaintColor = ContextCompat.getColor(mContext, R.color.color_main_black);
        mPadding = ScreenSizeUtils.dp2px(mContext, 9);

        if(attrs != null) {
            TypedArray a = mContext.obtainStyledAttributes(attrs, R.styleable.SymbolsTextViewAttrs);

            mTypes = a.getInteger(R.styleable.SymbolsTextViewAttrs_smbType, TYPE_MINUS);
            mPaintWidth = a.getDimensionPixelOffset(R.styleable.SymbolsTextViewAttrs_smbWith, mPaintWidth);
            mPaintColor = a.getColor(R.styleable.SymbolsTextViewAttrs_smbColor, mPaintColor);
            mPadding = a.getDimensionPixelOffset(R.styleable.SymbolsTextViewAttrs_smbPadding, mPadding);

            a.recycle();
        }
    }

    private void initData() {
        mPaint = new Paint();

        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(mPaintColor);
        mPaint.setStrokeWidth(mPaintWidth);
        mPaint.setAntiAlias(true);
    }

    public void setTextColor(int colorId) {
        if(mPaint != null) {
            mPaint.setColor(colorId);
            postInvalidate();
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int width;

        if (widthMode == MeasureSpec.EXACTLY) {
            //MeasureSpec.EXACTLY表示该view设置的确切的数值
            width = widthSize;
        } else {
            width = dp20;//默认值
        }

        //横线
        mHStartX = mPadding - 1;//减1是去掉view stroke
        mHStartY = (width - mPaintWidth) / 2;
        mHEndX = width - mPadding - 1;
        mHEndY = mHStartY;

        //竖线
        mVStartX = (width - mPaintWidth) / 2;
        mVStartY = mPadding - 1;//减1是去掉view stroke
        mVEndX = mVStartX;
        mVEndY = width - mPadding - 1;

        //这样做是因为加号宽高是相等的，手动设置宽高
        setMeasuredDimension(width, width);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if(mTypes == TYPE_MINUS) {
            canvas.drawLine(mHStartX, mHStartY, mHEndX, mHEndY, mPaint);
        } else if(mTypes == TYPE_PLUS) {
            canvas.drawLine(mHStartX, mHStartY, mHEndX, mHEndY, mPaint);
            canvas.drawLine(mVStartX, mVStartY, mVEndX, mVEndY, mPaint);
        }
    }

    public void setTypes(@SymbolsTypeDef int mTypes) {
        this.mTypes = mTypes;

        postInvalidate();
    }

    @IntDef({
            TYPE_PLUS,
            TYPE_MINUS,
    })
    @Target({
            ElementType.PARAMETER,
            ElementType.FIELD,
            ElementType.METHOD,
    }) //表示注解作用范围，参数注解，成员注解，方法注解
    @Retention(RetentionPolicy.SOURCE) //表示注解所存活的时间,在运行时,而不会存在 .class 文件中
    public @interface SymbolsTypeDef { //接口，定义新的注解类型
    }
}
